<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var sumSubarrayMins = function (A) {
      var sum = 0
      var lastmin
      for (let start = 0; start < A.length; start++) {
        lastmin = A[start]
        for (let end = start; end < A.length; end++) {
          var min = Math.min(lastmin, A[end])
          sum += min
          lastmin = min
        }
      }
      return sum % (10 ** 9 + 7)
    };
    console.log(sumSubarrayMins([3, 1, 2, 4]))
  </script>
</body>

</html>